import { Component, ViewChild } from '@angular/core';
import { NavParams, Events, AlertController, App, ModalController, NavController, Content, ActionSheetController,LoadingController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { Config } from '../../../providers/config';
import { DomSanitizer } from '@angular/platform-browser';
import _ from 'lodash';

import { Common } from "../../../providers/common";
import {BcwWarnService} from "./warn.service";
import {BcwHomeDetailPage} from "../home/detail.component";

@Component({
  selector: 'page-bcw-warn',
  templateUrl: './warn.html',
  providers: [BcwWarnService]
})
export class BcwWarnPage {
	moa_page_name: any = "page-bcw-warn";
  moa_page_explain: any = "预警消息";

  returnData = null;

  dataList = [];

  selectRule = "";

  returnRule = "";

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public config: Config,
    private sanitizer: DomSanitizer,
    public events: Events,
    public storage: Storage,
    private common: Common,
    private actionSheetCtrl: ActionSheetController,
    private bcwWarnService: BcwWarnService,
    private navParams: NavParams,
    private loadingCtrl:LoadingController
  ) {
    this.loadDatas();
  }

  ionViewDidEnter() {
    this.events.subscribe("BcwWarnPageRefresh", () => {
      this.loadDatas();
    });
  }

  loadDatas() {
    // let l_days = "5";
    // this.bcwWarnService.getMonitorData(l_days).subscribe(returnData => {
    //   console.log("getMonitorData", returnData);
    //   if (returnData) {
    //     this.returnData = returnData;
    //     this.selectRule = this.returnData ? this.returnData.rule[0] : "";
    //     this.dataList = this.returnData.data;
    //   }
    // });
    let l_days = '365';
    let vc_memo = '5';
    let loading = this.loadingCtrl.create({
      content: "加载中,请稍候...",
      // dismissOnPageChange: true
    });
    window['gfoa_loading'] = loading;
    loading.present();
    this.events.subscribe("loadingQJTDatas", () => {
      loading.dismiss();
      this.events.unsubscribe("loadingQJTDatas");
    });
    let list=[0,1,2,3,4,5];
    this.getExceptionRuleDetail(list,0,l_days,vc_memo,loading);

  }

  getExceptionRuleDetail(list,index,l_days,vc_memo,loading){
    if(index==list.length){
      loading.dismiss();
      console.log(this.dataList);
      console.log(this.selectRule);
      //默认拿去第一个规则select
      this.selectRule=this.returnRule[0];
      this.dataList=this.returnData[this.selectRule];
      return;
    }
    this.bcwWarnService.getExceptionRuleDetail(l_days,vc_memo).subscribe(returnData => {
      console.log("getExceptionRuleDetail", returnData);
      if (returnData.data) {
        // this.returnData = returnData;
        this.returnData=_.extend(this.returnData,returnData.data);
        // this.dataList.push(returnData.data);
      }
      if(returnData.rule){
        this.returnRule=this.returnRule.concat(returnData.rule);
      }
      index=index+1;
      return this.getExceptionRuleDetail(list,index,l_days,vc_memo,loading);
    });
  }

  viewDetail(data) {
    console.log("viewDetail.data", data);
    this.bcwWarnService.changeMonitorDataReadStatus(data).subscribe(returnData => {
      if (returnData && returnData.isSuccess) {
        data["isRead"] = true;
      }

      this.navCtrl.push(BcwHomeDetailPage, {
        detailData: data
      })
    });
  }

  selectRuleChange() {
    if (this.selectRule != this.returnData.rule[0]) {
      this.dataList = _.filter(this.returnData.data, {vc_rulename: this.selectRule});
    } else {
      this.dataList = this.returnData.data;
    }
  }

  moreOp(item) {
    let buttons = [];
    buttons.push({
      text: "选择优先级别",
      handler: () => {
        let inputs = [
          {
            name: "vc_memo",
            label: "1 普通",
            value: "1",
            checked: true,
            type: "checkbox"
          }, {
            name: "vc_memo",
            label: "2 一般",
            value: "2",
            checked: true,
            type: "checkbox"
          }, {
            name: "vc_memo",
            label: "3 较急",
            value: "3",
            checked: true,
            type: "checkbox"
          }, {
            name: "vc_memo",
            label: "4 紧急",
            value: "4",
            checked: true,
            type: "checkbox"
          }, {
            name: "vc_memo",
            label: "5 特急",
            value: "5",
            checked: true,
            type: "checkbox"
          }
        ];

        if (this.returnData.priority_levels) {
          _.forEach(inputs, input => {
              if (this.returnData.priority_levels.indexOf(input.value) === -1) {
                input.checked = false;
              }
          });
        }

        let alert = this.alertCtrl.create({
          title: '选择优先级别',
          inputs: inputs,
          buttons: [
            {
              text: '取消',
              role: 'cancel',
              handler: data => {

              }
            },
            {
              text: '确定',
              handler: data => {
                this.bcwWarnService.saveBcwPriorityLevels(data).subscribe(returnData => {
                  if (returnData && returnData.isSuccess) {
                    this.loadDatas();
                  }
                });
              }
            }
          ]
        });
        alert.present();
      }
    });
    buttons.push({
      text: "取消",
      role: 'cancel',
      handler: () => {
        // item.checked = false;
      }
    });

    let actionSheet = this.actionSheetCtrl.create({
      title: "更多操作",
      buttons: buttons
    });

    actionSheet.present();
  }
}
